//
//  NavigationBar.m
//  App
//
//  Created by ME on 16/5/20.
//  Copyright © 2016年 ME. All rights reserved.
//

#import "NavigationBar.h"
//#import "AppTheme.h"
#import "NSString+Extension.h"
@interface NavigationBar ()

@property (nonatomic, strong) UIView *statusBar;
@property (nonatomic, strong) UIView *bottomLine;
@property (nonatomic, strong) CAGradientLayer *gradientLayer;


@end

@implementation NavigationBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.statusBar = [UIView createViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SYS_STATUSBAR_HEIGHT) bgColor:nil alpha:1 superView:self];
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, NavigationBarHeight);            // 4s,5,6,6plus 的尺寸都是44
        // 屏蔽控制器view的tap事件，如有特殊需求，可进行特殊处理
        [self addTapGesture:^(UITapGestureRecognizer * _Nonnull tap) {
            
        }];
        
//        UIView *line = [UIView createViewWithBgColor:UIColorHex(0xeeeeee) alpha:1 superView:self];
//        line.sd_layout
//        .leftEqualToView(self)
//        .bottomEqualToView(self)
//        .rightEqualToView(self)
//        .heightIs(1);
    }
    return self;
}

#pragma mark - Privite

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel createLabelWithFont:UIFontMedium(18) text:self.controller.title superView:self];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = UIFontMedium(18);
        _titleLabel.textColor = UIColorWithRGBHex(0x333333);
    }
    return _titleLabel;
}

- (UIView *)bottomLine
{
    if (!_bottomLine) {
        _bottomLine = [UIView createViewWithFrame:CGRectMake(0, self.height - 1, SCREEN_WIDTH, 0.8) bgColor:UIColorWithRGBHex(0xeeeeee) alpha:1 superView:self];;
    }
    return _bottomLine;
}

- (CAGradientLayer *)gradientLayer
{
    if (!_gradientLayer) {
        _gradientLayer = [[CAGradientLayer alloc] init];
        _gradientLayer.frame = self.bounds;
        [self.layer addSublayer:_gradientLayer];
    }
    return _gradientLayer;
}

#pragma mark - 外部属性 & 方法
- (void)setBgImage:(UIImage *)bgImage
{
    _bgImage = bgImage;
    self.backgroundColor = [UIColor colorWithPatternImage:bgImage];
}

- (void)setBgColor:(UIColor *)bgColor
{
    _bgColor = bgColor;
    self.backgroundColor = bgColor;
    
    if (self.statusColor) {
        self.statusBar.backgroundColor = self.statusColor;
    } else {
        self.statusBar.backgroundColor = bgColor;
    }
}

- (void)setBackgroundColor:(UIColor *)backgroundColor
{
    [super setBackgroundColor:backgroundColor];
    if (self.statusColor) {
        self.statusBar.backgroundColor = self.statusColor;
    } else {
        self.statusBar.backgroundColor = backgroundColor;
    }
}

- (void)setStatusColor:(UIColor *)statusColor
{
    _statusColor = statusColor;
    self.statusBar.backgroundColor = _statusColor;
}

// 显示阴影
- (void)setShowShadow:(BOOL)showShadow
{
    _showShadow = showShadow;
    if (showShadow) {
        [self shadowRadius:3 shadowOffset:CGSizeMake(0, 1) shadowOpacity:0.5 shadowColor:[UIColor blackColor]];
        
    } else {
        self.layer.shadowOpacity = 0;
    }
}

// 显示不底部线
- (void)setShowLine:(BOOL)showLine
{
    _showLine = showLine;
    self.bottomLine.hidden = !_showLine;
}

// Title
- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titleLabel.text = title;
    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName : self.titleLabel.font}];
    self.titleLabel.size = size;
    self.titleLabel.x = (SCREEN_WIDTH - size.width)/2.0;
    self.titleLabel.y = SYS_STATUSBAR_HEIGHT + (44 - self.titleLabel.height)/2.0;
}

- (void)setTitleAttributions:(NSDictionary <NSString *, id> *)titleAttributions
{
    if ([self.title isNotNull]) {
        
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.title];
        [string addAttributes:titleAttributions range:NSRangeFromString(self.title)];
        self.titleLabel.attributedText = string;
    }
}

// leftView
- (void)setLeftView:(UIView *)leftView
{
    if (leftView) {
        [_leftView removeFromSuperview];
        _leftView = nil;
        _leftView = leftView;
        [self addSubview:leftView];
        
        _leftView.x = 15;
        _leftView.y = SYS_STATUSBAR_HEIGHT + (44 - _leftView.height)/2.0;
    }
}

// rightView
- (void)setRightView:(UIView *)rightView
{
    if (rightView) {
        [_rightView removeFromSuperview];
        _rightView = nil;
        _rightView = rightView;
        [self addSubview:rightView];
        
        _rightView.x = SCREEN_WIDTH - 15 - _rightView.width;
        _rightView.y = SYS_STATUSBAR_HEIGHT + (44 - _rightView.height)/2.0;
    }
}

// titleView
- (void)setTitleView:(UIView *)titleView
{
    if (titleView) {
        [_titleView removeFromSuperview];
        _titleView = nil;
        _titleView = titleView;
        [self addSubview:titleView];
        
        _titleView.centerX = SCREEN_WIDTH / 2.0;
        
        _titleView.y = SYS_STATUSBAR_HEIGHT + (44 - _titleView.height)/2.0;
    }
}

- (void)addGradientColors:(NSArray *)colors
{
    self.gradientLayer.colors = colors;;
}

@end
